/**********************************************************
 * ClassEditorAction.java.java
 * Created by Ashutosh
 * Last modified Date: Sep 18, 2008 10:46:23 AM by Ashutosh
 * Revision: <svn revision #>
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 **********************************************************/

package com.printeverywhere.action;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.log4j.Logger;
import org.apache.struts2.util.ServletContextAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.dto.ThemeManager;
import com.printeverywhere.util.FileUtil;

// TODO: Auto-generated Javadoc
/**
 * The Class ClassEditorAction.
 */
public class ClassEditorAction extends AdminLayoutIntegration implements ServletContextAware {

	/** Logger for this class. */
	private static final Logger logger = Logger.getLogger(ClassEditorAction.class);

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1L;
	
	private ServletContext servletContext;
	
	private List<ThemeManager> themeManagerList;
	
	private String fileContent;
	
	private List<String> fileName;
	
	private String selectFileName;
	
	private String themeName;
	
	public String getThemeName() {
		return themeName;
	}

	public void setThemeName(String themeName) {
		this.themeName = themeName;
	}

	public String getSelectFileName() {
		return selectFileName;
	}

	public void setSelectFileName(String selectFileName) {
		this.selectFileName = selectFileName;
	}

	public String getFileContent() {
		return fileContent;
	}

	public void setFileContent(String fileContent) {
		this.fileContent = fileContent;
	}

	/* (non-Javadoc)
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	public String execute() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		themeManagerList = (List<ThemeManager>) CommonFunctionDelegator.getAllfromtable(ThemeManager.class.getName());
		
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	public String showClassEditor() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("showClassEditor() - start"); //$NON-NLS-1$
		}
		
		themeName = "front";
		themeManagerList = (List<ThemeManager>) CommonFunctionDelegator.getAllfromtable(ThemeManager.class.getName());
		fileName = FileUtil.getFileNameList(servletContext.getRealPath("panels"));
		if(fileName != null){
			selectFileName = fileName.get(0);
			fileContent = FileUtil.readFile(servletContext.getRealPath("panels")+"/"+selectFileName);
		}
		
		if (logger.isDebugEnabled()) {
			logger.debug("showClassEditor() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	public String showTemplateEditor() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("showTemplateEditor() - start"); //$NON-NLS-1$
		}
		themeName = "front";
		themeManagerList = (List<ThemeManager>) CommonFunctionDelegator.getAllfromtable(ThemeManager.class.getName());
		fileName = FileUtil.getFileNameList(servletContext.getRealPath("jsp")+"/front");
		if(fileName != null){
			selectFileName = fileName.get(0);
			fileContent = FileUtil.readFile(servletContext.getRealPath("jsp")+"/front/"+selectFileName);
		}
		
		if (logger.isDebugEnabled()) {
			logger.debug("showTemplateEditor() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String showSelectTemplate() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("showSelectTemplate() - start"); //$NON-NLS-1$
		}
		String path = null;
		if(themeName != null){
			if(themeName.equalsIgnoreCase("front")){
				path = servletContext.getRealPath("jsp")+"\\front";
				fileName = FileUtil.getFileNameList(path);
			}else{
				path = servletContext.getRealPath("jsp")+"\\front\\"+themeName;
				fileName = FileUtil.getFileNameList(path);
			}
			if(fileName != null){
				fileContent = FileUtil.readFile(path+"\\"+selectFileName);
			}
		}
		themeManagerList = (List<ThemeManager>) CommonFunctionDelegator.getAllfromtable(ThemeManager.class.getName());
		
		if (logger.isDebugEnabled()) {
			logger.debug("showSelectTemplate() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}

	public String showSelectTheme() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("showSelectTheme() - start"); //$NON-NLS-1$
		}
		
		String path = null;
		themeManagerList = (List<ThemeManager>) CommonFunctionDelegator.getAllfromtable(ThemeManager.class.getName());
		if(themeName != null){
			if(themeName.equalsIgnoreCase("front")){
				path = servletContext.getRealPath("jsp")+"\\front";
				fileName = FileUtil.getFileNameList(path);
			}else{
				path = servletContext.getRealPath("jsp")+"\\front\\"+themeName;
				fileName = FileUtil.getFileNameList(path);
			}
			if(fileName != null){
				selectFileName = fileName.get(0);
				fileContent = FileUtil.readFile(path+"\\"+selectFileName);
			}
		}
		
		
		if (logger.isDebugEnabled()) {
			logger.debug("showSelectTheme() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	public String saveFile() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("saveFile() - start"); //$NON-NLS-1$
		}
		
		String path = null;
		if(themeName != null){
			if(themeName.equalsIgnoreCase("front")){
				path = servletContext.getRealPath("jsp")+"\\front\\"+selectFileName;
			}else{
				path = servletContext.getRealPath("jsp")+"\\front\\"+themeName+"\\"+selectFileName;
			}
			try{
				FileUtil.writeFile(path, fileContent);
				addActionMessage("Save change successfully");
			}catch (Exception e) {
				e.printStackTrace();
				addActionError("Error while updating file");
				return INPUT;
			}
		}
		
		
		if (logger.isDebugEnabled()) {
			logger.debug("saveFile() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	public String showThemeClass() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("showThemeClass() - start"); //$NON-NLS-1$
		}
		
		String path = null;
		themeManagerList = (List<ThemeManager>) CommonFunctionDelegator.getAllfromtable(ThemeManager.class.getName());
		if(themeName != null){
			if(themeName.equalsIgnoreCase("front")){
				path = servletContext.getRealPath("panels");
				fileName = FileUtil.getFileNameList(path);
			}else{
				path = servletContext.getRealPath("panels")+"\\"+themeName;
				fileName = FileUtil.getFileNameList(path);
			}
			if(fileName != null){
				selectFileName = fileName.get(0);
				fileContent = FileUtil.readFile(path+"\\"+selectFileName);
			}
		}
		
		
		if (logger.isDebugEnabled()) {
			logger.debug("showThemeClass() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	
	public String showTemplateClass() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("showTemplateClass() - start"); //$NON-NLS-1$
		}
		String path = null;
		if(themeName != null){
			if(themeName.equalsIgnoreCase("front")){
				path = servletContext.getRealPath("panels");
				fileName = FileUtil.getFileNameList(path);
			}else{
				path = servletContext.getRealPath("panels")+"\\"+themeName;
				fileName = FileUtil.getFileNameList(path);
			}
			if(fileName != null){
				fileContent = FileUtil.readFile(path+"\\"+selectFileName);
			}
		}
		themeManagerList = (List<ThemeManager>) CommonFunctionDelegator.getAllfromtable(ThemeManager.class.getName());
		
		if (logger.isDebugEnabled()) {
			logger.debug("showTemplateClass() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
	
	public String saveClassFile() throws Exception{
		if (logger.isDebugEnabled()) {
			logger.debug("saveClassFile() - start"); //$NON-NLS-1$
		}
		
		String path = null;
		if(themeName != null){
			if(themeName.equalsIgnoreCase("front")){
				path = servletContext.getRealPath("panels")+"\\"+selectFileName;
			}else{
				path = servletContext.getRealPath("panels")+"\\"+themeName+"\\"+selectFileName;
			}
			try{
				FileUtil.writeFile(path, fileContent);
				addActionMessage("Save change successfully");
			}catch (Exception e) {
				e.printStackTrace();
				addActionError("Error while updating file");
				return INPUT;
			}
		}
		
		
		if (logger.isDebugEnabled()) {
			logger.debug("saveClassFile() - end"); //$NON-NLS-1$
		}
		return SUCCESS;
	}
		
	public List<String> getFileName() {
		return fileName;
	}

	public void setFileName(List<String> fileName) {
		this.fileName = fileName;
	}

	public List<ThemeManager> getThemeManagerList() {
		return themeManagerList;
	}

	public void setThemeManagerList(List<ThemeManager> themeManagerList) {
		this.themeManagerList = themeManagerList;
	}

	public void setServletContext(ServletContext servletContext) {
		this.servletContext = servletContext;
	}
	
}